package com.tarena.lbs.basic.web.service;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.filters.ThreadLocalUtils;
import com.tarena.lbs.basic.web.repository.AdminRepository;
import com.tarena.lbs.basic.web.repository.BusinessRepository;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.dto.AdminDTO;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminService {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private BusinessRepository businessRepository;
    public AdminDTO adminDetail(Integer adminId)throws BusinessException {
        AdminPO po=adminRepository.getAdminById(adminId);
        AdminDTO dto=new AdminDTO();
        if (po!=null&&po.getId()!=null){
            BeanUtils.copyProperties(po,dto);
        }
        return dto;
    }
    public AdminVO adminDetail() throws BusinessException {
        //1.下游线程代码 如果过滤器成功解析jwt 可以从threadlocal获取登录用户
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证失败"));
        //2. 从认证数据拿到id
        Integer id = userPrinciple.getId();
        AdminPO po=adminRepository.getAdminById(id);
        AdminVO vo=new AdminVO();
        if (po!=null&&po.getId()!=null){
            BeanUtils.copyProperties(po,vo);
        }
        return vo;
    }

    public PageResult<AdminVO> adminPage(AdminQuery query) {
        //1.使用条件查询仓储层pageInfo
        PageInfo<AdminPO> poPageInfo= adminRepository.getAdminByPage(query);
        //2.封装pageResult pageNo pageSize total object(分页数据)
        PageResult<AdminVO> voPages=new PageResult<>();
        voPages.setPageNo(poPageInfo.getPageNum());
        voPages.setPageSize(poPageInfo.getPageSize());
        voPages.setTotal(poPageInfo.getTotal());
        List<AdminVO> vos=null;
        if (poPageInfo.getTotal()>0){
            //stream()
            vos=poPageInfo.getList().stream().map(po->{
                AdminVO vo=new AdminVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPages.setObjects(vos);
        return voPages;
    }


    public void adminSave(AdminParam param) throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证失败"));
        Asserts.isTrue(userPrinciple.getRole()!= Roles.ADMIN,new BusinessException("-2","用户权限不足"));
        //判断校验businessId商家是否存在
        if (param.getAccountType()==1) {
            BusinessPO poBiz = businessRepository.getBusiness(param.getBusinessId());
            Asserts.isTrue(poBiz == null, new BusinessException("-2", "商家不存在"));
        }
        //1.幂等 新增insert 先查 后增(也可以将手机号 email做成唯一字段 try catch数据异常)
        Long total=adminRepository.countByPhoneOrEmail(param);
        Asserts.isTrue(total>0,new BusinessException("-2","手机号或邮箱已存在"));
        //2.补充数据新增
        AdminPO poParam=new AdminPO();
        BeanUtils.copyProperties(param,poParam);
        poParam.setAccountStatus(0);//启用0  禁用1
        poParam.setBusinessId(param.getAccountType()==0?0:param.getBusinessId());
        poParam.setCreateTime(new Date());
        adminRepository.saveAdmin(poParam);
    }
}


























